home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / gopher.lha / gopher1.01 / gopherd / Waisindex.c < prev   
C/C++ Source or Header  |  1992-06-22  |  7KB  |  287 lines

  1. #ifdef WAISSEARCH
  2.  
  3. /* WIDE AREA INFORMATION SERVER SOFTWARE
  4.    No guarantees or restrictions.  See the readme file for the full standard
  5.    disclaimer.    
  6.    Brewster@think.com
  7.  
  8.    Heavily hacked by Paul Lindner (lindner@boombox.micro.umn.edu)
  9.    Do you even recognize this Brewster? :-)
  10.  
  11. */
  12.  
  13. int ShowDate = 0;
  14.  
  15. #define _search_c
  16.  
  17. #include <ctype.h>
  18.  
  19. #ifndef ultrix
  20. #include <string.h>     /* for strlen() */
  21. #endif
  22.  
  23. #include "../ir/cutil.h"
  24. #include "../ir/irfiles.h"
  25. #include "../ir/irlex.h"
  26. #include "../ir/irext.h"
  27. #include "../ir/irsearch.h"
  28. #include "../ir/docid.h"
  29. #include <math.h>
  30.  
  31.  
  32. #include "gopherd.h"
  33.  
  34.  
  35. FILE *logfile = NULL; /* the logfile */
  36. char *log_file_name = NULL;
  37.  
  38. static char *DefaultDB = "index";
  39. static char *MonthStr[] = {
  40.      "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Sept", "Oct",
  41.       "Nov", "Dec"
  42. };
  43.  
  44. void
  45. WaisIndexQuery(sockfd, index_directory, SearchWords, new_db_name, INDEXHost, INDEXPort, INDEXPath)
  46.   int sockfd;
  47.   char *index_directory;
  48.   char *SearchWords;
  49.   char *new_db_name;
  50.   char *INDEXHost;
  51.   int  INDEXPort;
  52.   char *INDEXPath;
  53.      database* db;
  54.      long maxRawScore;
  55.      long normalScore;
  56.      char *cp;
  57.      char dateline[10];
  58.      long i;
  59.      query_parameter_type parameters;
  60.      boolean search_result;
  61.      char score[6];
  62.      static char ReturnLine[512];
  63.      float  closeness;
  64.      GopherDirObj *gd;
  65.      GopherObj    *gs;
  66.      
  67.  
  68.      gs = GSnew();
  69.      gd = GDnew(32);
  70.  
  71.      if (DEBUG)  {
  72.       fprintf(stderr, "IndexPath: %s\n", INDEXPath);
  73.       logfile = stderr;   /** Log wais error messages to console **/
  74.      }     else {
  75.       logfile = ufopen("/dev/null", "w+");
  76.      }
  77.  
  78.      if (new_db_name == NULL) {
  79.       new_db_name = DefaultDB;
  80.      }
  81.  
  82.      if (uchdir(index_directory)) {
  83.       Abortoutput(sockfd, "Couldn't change to index directory...");
  84.       return;
  85.      }
  86.  
  87.      if (SearchWords != NULL && strlen(SearchWords) == 0) {
  88.       EveryWAISdocument(new_db_name);
  89.       return;
  90.      }
  91.  
  92.      db = openDatabase(new_db_name, false, true);
  93.      
  94.      if (db == NULL) {
  95.       sprintf(ReturnLine, "Failed to open database %s in index dir %s", new_db_name, index_directory);
  96.       Abortoutput(sockfd, ReturnLine);
  97.       writestring(sockfd, ".\r\n"); /** be polite **/
  98.       return;
  99.      }
  100.      
  101.      parameters.max_hit_retrieved = 256;
  102.  
  103.      set_query_parameter(SET_MAX_RETRIEVED_MASK, ¶meters);
  104.      
  105.      search_result = false;
  106.      search_result |= search_for_words(SearchWords, db, 0);
  107.      
  108.      if (search_result == true) {
  109.       /* the search went ok */
  110.       hit best_hit;
  111.       
  112.       finished_search_word(db);
  113.       if (DEBUG)
  114.            printf("After finished_search\n");
  115.  
  116.       for (i = 0; i < 200; i++){ 
  117.            if (0 != next_best_hit(&best_hit, db))
  118.             break;        /* out of hits */
  119.            if (i == 0)
  120.             maxRawScore = best_hit.weight;
  121.            if (best_hit.weight > 0 && 
  122.            strstr(best_hit.filename, ".cache")==NULL){
  123.             long lines,length;
  124.  
  125.             char** type = NULL;
  126.             
  127.             normalScore = (long)floor((((double)best_hit.weight) /
  128.                            ((double)maxRawScore)) *    
  129.                           (MAX_NORMAL_SCORE + 1));
  130.  
  131.             if (normalScore > MAX_NORMAL_SCORE)
  132.              normalScore = MAX_NORMAL_SCORE;
  133.             
  134.  
  135.             /*** Strip off the first part of the path in the filename*/
  136.             /*** Plus it gets rid of weird automount things... ***/
  137.             cp =strstr(best_hit.filename, INDEXPath);
  138.             if (cp == NULL)
  139.              cp = "Error in Hostdata!";
  140.             else
  141.              cp += strlen(INDEXPath);
  142.             
  143.  
  144.                     sprintf(score,"%3d ",best_hit.weight);
  145.  
  146.                     waislog(0,99,"%s: Score %3d:%s",SearchWords,best_hit.weight,cp);
  147.             
  148.             /** Make the outgoing string **/
  149.  
  150.             ZapCRLF(best_hit.headline);
  151.             
  152.             if (ShowDate) {
  153.              sprintf(dateline, "%.2s/%.2s/%.2s ",
  154.                  best_hit.date + 2, best_hit.date + 4,
  155.                  best_hit.date);
  156.             }
  157.             
  158.             GSsetType(gs, '0');
  159.             GSsetTitle(gs, best_hit.headline);
  160.             GSsetHost(gs, INDEXHost);
  161.             GSsetPort(gs, INDEXPort);
  162.             
  163.             sprintf(ReturnLine, "R%d-%d-/%s",
  164.                 best_hit.start_character, best_hit.end_character,
  165.                 cp);
  166.             
  167.             if (!MacIndex)
  168.             GSsetPath(gs, ReturnLine);
  169.             else
  170.             GSsetPath(gs, cp);
  171.             GSsetWeight(gs, best_hit.weight);
  172.             
  173.             GDaddGS(gd, gs);
  174.            }
  175.            
  176.            
  177.            if (DEBUG) {
  178.             printf("%s\n", ReturnLine);
  179.             printf("End Byte   = %d\n", best_hit.end_character);
  180.             printf("Doc length = %d\n", best_hit.document_length);
  181.             printf("#lines     = %d\n", best_hit.number_of_lines);
  182.            }
  183.       }
  184.      }
  185.      else {
  186.       /* something went awry in the search */
  187.       LOGGopher(sockfd, "Something went wrong in the search!\r\n");
  188.       writestring(sockfd, ".\r\n"); /*** be polite, don't screw up the client**/
  189.       return;
  190.      }
  191.      finished_best_hit(db);
  192.  
  193.      /*** Finish the output ***/
  194.      /*** End with the period... ***/
  195.  
  196.      if (UsingHTML)
  197.       GDtoNetHTML(gd, sockfd);
  198.      else
  199.       GDtoNet(gd, sockfd);
  200.  
  201.      writestring(sockfd, ".\r\n");
  202.  
  203.      /* free everything */
  204.      closeDatabase(db);
  205.      return;
  206. }
  207.  
  208. EveryWAISdocument(sockfd, db, INDEXHost, INDEXPort, INDEXPath)
  209.   int sockfd;
  210.   char *db;
  211.   char *INDEXHost;
  212.   int  INDEXPort;
  213.   char *INDEXPath;
  214. {
  215.      FILE         *dbcatalog;
  216.      char         db_name[MAXPATHLEN];
  217.      char         inputline[512];
  218.      String       *Headline;
  219.      String       *Filename;
  220.      int          StartByte, EndByte;
  221.      GopherObj    *gs;
  222.      GopherDirObj *gd;
  223.      boolean      Headlineset = FALSE;
  224.      boolean      DocIDset    = FALSE;
  225.  
  226.      gs = GSnew();
  227.      gd = GDnew();
  228.      Headline = STRnew();
  229.      Filename = STRnew();
  230.  
  231.      strcpy(db_name, db);
  232.      strcat(db_name, ".cat");
  233.  
  234.      dbcatalog = rfopen(db_name, "r");
  235.      
  236.      while (fgets(inputline, sizeof(inputline), dbcatalog) != NULL) {
  237.       if (strncmp(inputline, "Headline: ", 10)==0) {
  238.            STRset(Headline, inputline +10);
  239.            Headlineset = TRUE;
  240.       }
  241.       else if (strncmp(inputline, "DocID: ", 7)==0) {
  242.            char *cp;
  243.  
  244.            StartByte = atoi(inputline);
  245.            cp = strchr(inputline+7, ' ');
  246.            if (cp == NULL) break;
  247.  
  248.            cp++;
  249.            EndByte = atoi(cp);
  250.  
  251.            cp = strchr(inputline+7, ' ');
  252.            cp++;
  253.            if (cp == NULL) break;
  254.  
  255.            cp =strstr(cp, INDEXPath);
  256.            if (cp == NULL) break;
  257.            
  258.            STRset(Filename, cp);
  259.  
  260.            DocIDset = TRUE;
  261.       }
  262.       
  263.       if (DocIDset == TRUE && Headlineset == TRUE) {
  264.            char tmppath[512];
  265.  
  266.            sprintf(tmppath, "R%d-%d-%s", StartByte, EndByte, STRget(Filename));
  267.  
  268.            GSsetType(gs, '0');
  269.            GSsetTitle(gs, STRget(Headline));
  270.            GSsetHost(gs, INDEXHost);
  271.            GSsetPort(gs, INDEXPort);
  272.            GSsetPath(gs, tmppath);
  273.  
  274.            GDaddGS(gd, gs);
  275.  
  276.            DocIDset = FALSE;
  277.            Headlineset = FALSE;
  278.       }
  279.      }      
  280. }
  281.  
  282.  
  283. #endif /** WAISSEARCH **/
  284.  
  285.  
  286.